<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  
  <title>path | H5教案</title>

  
  <meta name="author" content="John Doe">
  

  
  <meta name="description" content="6、path路径主要是为了解决本地路径的拼接所需要的一个方法集
引入path1var path = require(&amp;#x27;path&amp;#x27;);
path.join方法使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起，然后规范化生成的路径。
12var str=path.">
  

  
  
  <meta name="keywords" content="">
  

  <meta id="viewport" name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no, minimal-ui">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">

  <meta property="og:title" content="path"/>

  <meta property="og:site_name" content="H5教案"/>

  
  <meta property="og:image" content="/ericxie-book/favicon.ico"/>
  

  <link href="/ericxie-book/favicon.ico" rel="icon">
  <link rel="alternate" href="/ericxie-book/atom.xml" title="H5教案" type="application/atom+xml">
  <link rel="stylesheet" href="/ericxie-book/css/style.css" media="screen" type="text/css">
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
<div class="blog">
  <div class="content">

    <header>
  <div class="site-branding">
    <h1 class="site-title">
      <a href="/ericxie-book/">H5教案</a>
    </h1>
    <p class="site-description"></p>
  </div>
  <nav class="site-navigation">
    <ul>
      
    </ul>
  </nav>
</header>

    <main class="site-main posts-loop">
    <article>

  
    
    <h3 class="article-title"><span>path</span></h3>
    
  

  <div class="article-top-meta">
    <span class="posted-on">
      <a href="/ericxie-book/2021/01/26/path/" rel="bookmark">
        <time class="entry-date published" datetime="2021-01-26T07:03:18.000Z">
          2021-01-26
        </time>
      </a>
    </span>
  </div>


  

  <div class="article-content">
    <div class="entry">
      
        <h2 id="6、path"><a href="#6、path" class="headerlink" title="6、path"></a>6、path</h2><p>路径主要是为了解决本地路径的拼接所需要的一个方法集</p>
<h3 id="引入path"><a href="#引入path" class="headerlink" title="引入path"></a>引入path</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">var path = require(<span class="string">&#x27;path&#x27;</span>);</span><br></pre></td></tr></table></figure>
<h4 id="path-join"><a href="#path-join" class="headerlink" title="path.join"></a>path.join</h4><p>方法使用平台特定的分隔符作为定界符将所有给定的 path 片段连接在一起，然后规范化生成的路径。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf&#x27;</span>);</span><br><span class="line">console.log(str);</span><br></pre></td></tr></table></figure>
<h4 id="path-basename"><a href="#path-basename" class="headerlink" title="path.basename"></a>path.basename</h4><p>返回路径中最后的部分,可以去掉扩展名。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf.txt&#x27;</span>);</span><br><span class="line">console.log(path.basename(str));</span><br><span class="line">console.log(path.basename(str,<span class="string">&quot;.txt&quot;</span>))</span><br></pre></td></tr></table></figure>
<h4 id="path-dirname"><a href="#path-dirname" class="headerlink" title="path.dirname"></a>path.dirname</h4><p>返回路径中目录名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf.txt&#x27;</span>);</span><br><span class="line">console.log(path.dirname(str));</span><br></pre></td></tr></table></figure>
<h4 id="path-extname"><a href="#path-extname" class="headerlink" title="path.extname"></a>path.extname</h4><p>返回路径中的扩展名</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf.txt&#x27;</span>);</span><br><span class="line">console.log(path.extname(str));</span><br></pre></td></tr></table></figure>
<h4 id="path-parse"><a href="#path-parse" class="headerlink" title="path.parse"></a>path.parse</h4><p>将字符串对象转换为对象</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf.txt&#x27;</span>);</span><br><span class="line">console.log(path.parse(str));</span><br></pre></td></tr></table></figure>
<h4 id="path-format"><a href="#path-format" class="headerlink" title="path.format"></a>path.format</h4><p>将path对象转换为字符串</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var str=path.join(<span class="string">&#x27;/foo&#x27;</span>, <span class="string">&#x27;bar&#x27;</span>, <span class="string">&#x27;baz/asdf.txt&#x27;</span>);</span><br><span class="line">var obj=path.parse(str);</span><br><span class="line">console.log(path.format(obj))</span><br></pre></td></tr></table></figure>
<h4 id="dirname和-filename"><a href="#dirname和-filename" class="headerlink" title="__dirname和__filename"></a>__dirname和__filename</h4><p>本地全局的当前文件夹地址和全局的当前文件路径和文件名</p>

      
    </div>

  </div>

  <div class="article-footer">
    <div class="article-meta pull-left">

    

    

    </div>

    
  </div>
</article>

  






    </main>

    <footer class="site-footer">
  <p class="site-info">
    Proudly powered by <a href="https://hexo.io/" target="_blank">Hexo</a> and
    Theme by <a href="https://github.com/CodeDaraW/Hacker" target="_blank">Hacker</a>
    </br>
    
    &copy; 2021 John Doe
    
  </p>
</footer>
    
    
  </div>
</div>
</body>
</html>